// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: "yiyuanfan-0g6e8n6n8ab2decd"
})

// 云函数入口函数
exports.main = async (event, context) => {
  switch (event.action) {
    case 'rootlogin': {  //窗口阿姨登录
      return await root_login()
    }
    case 'meal': { //根据canteenid和windowid去查询菜单
      return await meal(event)
    }
    case 'mealbyid': { //根据id去查询菜单
      return await meal_by_id(event)
    }
    case 'modifystatus': {  //修改meal菜单的上架下架的状态
      return await modify_status(event)
    }
    case 'delmeal': {  //删除菜单
      return await del_meal(event)
    }
    case 'addmeal': {  //增加一个菜单的信息
      return await add_meal(event)
    }
    case 'editmeal': {  //增加一个菜单的信息
      return await edit_meal(event)
    }
    case 'findcanteen': {  //查找一个餐厅的信息通过canteenid
      return await find_canteen(event)
    }
    case 'findwindow': {  //查找一个窗口的信息通过windowid
      return await find_window(event)
    }
    case 'mealbycanteenid': {  //通过canteenid查找meal信息
      return await meal_by_canteenid(event)
    }
    default: {  //其他默认返回空，没有数据
      return
    }
  }
}


async function root_login() {
  return cloud.database().collection("canteen_window")
  .get()
  .then(res => {
    return res
  })
  .catch(err => {
    return err
  });;
}

async function meal(event) {
  return cloud.database().collection("meal")
  .where({
    window_id: event.window_id,
    canteen_id: event.canteen_id
    // window_id: "1",
    // canteen_id: "1"
  })
  .get()
  .then(res => {
    return res
  })
  .catch(err => {
    return err
  });;
}

async function meal_by_id(event) {
  return cloud.database().collection("meal")
  .where({
    _id: event._id
  })
  .get()
  .then(res => {
    return res
  })
  .catch(err => {
    return err
  });;
}


async function modify_status(event) {
  return cloud.database().collection("meal")
  .doc(event._id).update({
    data: {
      meal_status: event.status
    }
  })
  .then(res => {
    return res
  })
  .catch(err => {
    return err
  });
}

async function del_meal(event) {
  return cloud.database().collection("meal")
  .doc(event._id)
  .remove()
  .then(res => {
    return res
  })
  .catch(err => {
    return err
  });
  
}


async function add_meal(event) {
  return cloud.database().collection("meal")
  .add({
    data: {
      meal_name: event.meal_name,
      meal_num: event.meal_num,
      meal_price: event.meal_price,
      meal_detail: event.meal_detail,
      meal_img: event.meal_img,
      meal_status: event.meal_status,
      window_id: event.window_id,
      canteen_id: event.canteen_id
    }
  })
  .then(res => {
    return res
  })
  .catch(err => {
    return err
  });
  
}

async function edit_meal(event) {
  return cloud.database().collection("meal").doc(event._id)
  .update({
    data: {
      meal_name: event.meal_name,
      meal_num: event.meal_num,
      meal_price: event.meal_price,
      meal_detail: event.meal_detail,
      meal_img: event.meal_img,
      meal_status: event.meal_status,
      window_id: event.window_id,
      canteen_id: event.canteen_id
    }
  })
  .then(res => {
    return res
  })
  .catch(err => {
    return err
  });
  
}


async function find_canteen(event) {
  return cloud.database().collection("canteen")
  .where({
    canteen_id: event.canteen_id
  })
  .get()
  .then(res => {
    return res
  })
  .catch(err => {
    return err
  });;
}

async function find_window(event) {
  return cloud.database().collection("window")
  .where({
    window_id: event.window_id,
  })
  .get()
  .then(res => {
    return res
  })
  .catch(err => {
    return err
  });
}

async function meal_by_canteenid(event) {
  return cloud.database().collection('meal').aggregate()
  .match({
    meal_status: false,
    canteen_id: event.canteen_id
  })
  .lookup({
    from: 'window',
    localField: 'window_id',
    foreignField: 'window_id',
    as: 'window'
  })
  .lookup({
    from: 'canteen',
    localField: 'canteen_id',
    foreignField: 'canteen_id',
    as: 'canteen'
  }).end()
  .then(res => {
    const meals = [];
    for(let mealdata of res.list){
      let meal = {}
      meal._id = mealdata._id
      meal.meal_name = mealdata.meal_name
      meal.meal_num = mealdata.meal_num
      meal.meal_status = mealdata.meal_status
      meal.meal_price = mealdata.meal_price
      meal.meal_img = mealdata.meal_img
      meal.canteen_name = mealdata.canteen[0].canteen_name
      meal.window_name = mealdata.window[0].window_name
      meals.push(meal)
    }
    return meals
  })
  .catch()
}

